package F21AS_Freddy.AFJ_Taxikiosk.src;

//package queue;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * Controller to start the queue in a separate thread and to handle user events
 * from the QueueGUI class
 * 
 * @author Andrew Stokes
 * 
 */
public class QueueController {

	private Queue queue;
	private QueueGUI queueGUI;

	public QueueController(Queue queue, QueueGUI queueGUI) {
		this.queue = queue;

		this.queueGUI = queueGUI;
		queueGUI.startQueueButtonListener(new BuildQueueButtonListener());
		queueGUI.startQueueSpeedSliderListener(new QueueSpeedSliderListener());
		queueGUI.startMoreQueueButtonListener(new MoreQueueButtonListener());

	}

	public class BuildQueueButtonListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			
			queueGUI.setStartQueueButtonEnabled(false);
			Thread thread = new Thread(queue);
			thread.start();
			
		}
	}

	public class QueueSpeedSliderListener implements ChangeListener {

		public void stateChanged(ChangeEvent e) {
			JSlider source = (JSlider) e.getSource();
			if (!source.getValueIsAdjusting()) {

				// set the speed of the queue
				queue.setQueueSpeed(source.getValue());
			}

		}
	}
	public class MoreQueueButtonListener implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			String input = "";
			String [] values = new String[4];
			input = JOptionPane.showInputDialog(null, "Enter name, "
					+ "reference, passenger number and Id separated "
					+ "by ,");
			values = input.split(",");
			
			int num = Integer.parseInt(values[2]);
			int kiosk = Integer.parseInt(values[3]);
			Passenger p = new Passenger(values[0],values[1],num,kiosk);
			try {
				queue.addQueue(p);
				JOptionPane.showMessageDialog(null, "details inserted");
				
			} catch (DuplicatePassengerException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			}
		}
		
}	
	
